﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;

namespace GemBlaster
{
    public class SavedGameData
    {
        const string HighScoreFileName = "highscore.dat";

        public static long GetHighScore()
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoStore.GetFileNames(HighScoreFileName).Length > 0)
                {
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("highscore.dat", FileMode.Open, isoStore))
                    {
                        using (StreamReader reader = new StreamReader(isoStream))
                        {
                            String data = reader.ReadToEnd();
                            try
                            {
                                return Int64.Parse(data);
                            }
                            catch (Exception)
                            {
                                return 0;
                            }
                        }
                    }
                }
                else
                    return 0;
            }
        }

        public static void SaveHighScore(long score)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(HighScoreFileName, FileMode.OpenOrCreate, isoStore))
                {
                    using (StreamWriter writer = new StreamWriter(isoStream))
                    {
                        writer.Write(score.ToString());
                    }
                }
            }
        }
    }
}
